package com.guoyu.gserv.dao.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.guoyu.gserv.dao.NewsDao;
import com.guoyu.gserv.entity.News;

public class NewsImpi implements NewsDao {
	private SessionFactory sessionFactory;
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	public Session getSession()
	{
		return sessionFactory.getCurrentSession();
	}
	public void addNews(News news) {
		// TODO Auto-generated method stub
		getSession().save(news);
	}

	public void delNews(News news) {
		// TODO Auto-generated method stub
		getSession().delete(news);
	}
	
	public void upNews(News news) {
		// TODO Auto-generated method stub
		getSession().update(news);
	}
	
	public List<News> selectNews(int newTypeId) {
		// TODO Auto-generated method stub
		Criteria c = getSession().createCriteria(News.class);
		c.add(Restrictions.eq("newType.newTypeId", newTypeId));
		List<News> list = c.list();
		return list;
	}

	public News selectNewsById(int newsId) {
		// TODO Auto-generated method stub
		return (News)getSession().load(News.class, newsId);
	}
	public List<News> selectNewsByTypeId(int newTypeId){
		Criteria c = getSession().createCriteria(News.class);
		c.add(Restrictions.eq("newType.newTypeId", newTypeId));
		c.setFirstResult(0);
		c.setMaxResults(5);
		List<News> list = c.list();
		return list;
	}

	

	

}
